package com.kuaimacode.kframework.agent.services.finance;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kuaimacode.kframework.agent.services.agent.SysAgentService;
import com.kuaimacode.kframework.agent.util.SessionUtil;
import com.kuaimacode.kframework.common.Constant;
import com.kuaimacode.kframework.mybatis.mapper.finance.AgentDrawApplyMapper;
import com.kuaimacode.kframework.mybatis.models.agent.SysAgent;
import com.kuaimacode.kframework.mybatis.models.finance.AgentAccountDetail;
import com.kuaimacode.kframework.mybatis.models.finance.AgentDrawApply;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author anzy
 * @date 2019/05/23
 */
@Service
public class AgentDrawApplyService extends ServiceImpl<AgentDrawApplyMapper, AgentDrawApply> {


	@Autowired
	private SysAgentService sysAgentService;

	@Autowired
	private AgentAccountDetailService agentAccountDetailService;


    	/**
    	 *  分页查找所有的数据
    	 * @param pageNum 当前页码，从1开始
    	 * @param pageSize  一页显示多少行
    	 * @return
    	 */
    	public PageInfo<AgentDrawApply> findAll(String status,int pageNum, int pageSize) {
    		PageHelper.startPage(pageNum, pageSize);
			QueryWrapper queryWrapper = new QueryWrapper();
			queryWrapper.eq("agent_id",SessionUtil.getAgentId());
			if(status!=null&&!"".equals(status)) {
				queryWrapper.eq("status",status);
			}
			queryWrapper.orderByDesc("create_at");
    		List<AgentDrawApply> datas = baseMapper.selectList(queryWrapper);
    		return new PageInfo<AgentDrawApply>(datas);
    	}

    	@Transactional
    	public void saveDrawApply(AgentDrawApply agentDrawApply){

			SysAgent agent = sysAgentService.getById(SessionUtil.getAgentId());
			// 新建保存
			agentDrawApply.setAgentId(SessionUtil.getAgentId());
			agentDrawApply.setCreateAt(new Date());
			agentDrawApply.setCreateBy(SessionUtil.getUserId());
			agentDrawApply.setStatus(1);
			baseMapper.insert(agentDrawApply);
			//更新账户余额
			sysAgentService.updateBal(SessionUtil.getAgentId(), Constant.InOutEnum.OUT.getCode().toString(),agentDrawApply.getAmount());
			//插入账户明细表
			AgentAccountDetail detail = new AgentAccountDetail();
			detail.setTranType(Constant.AgentAccountTranTypeEnum.DRAW.getCode().toString());
			detail.setInOut(Constant.InOutEnum.OUT.getCode().toString());
			detail.setAmount(agentDrawApply.getAmount());
			detail.setTranDesc("提现");
			detail.setAgentId(SessionUtil.getAgentId());
			detail.setTranBal(agent.getBalance().subtract(agentDrawApply.getAmount()));
			detail.setTranId(agentDrawApply.getId().toString());
			agentAccountDetailService.insertAgentAccountDetail(detail);
			//TODO 平台的收益，在平台确认转账成功后写入？


		}

	/**
	 * 取消申请
	 * @param id
	 * @return
	 */
	@Transactional
	public String cancleApply(String id){

		//判断提现申请的状态是否变更
		AgentDrawApply agentDrawApply = baseMapper.selectById(id);
		if(agentDrawApply.getStatus() == 1){
			//更新提现申请状态为取消
			UpdateWrapper updateWrapper = new UpdateWrapper();
			updateWrapper.eq("status",1);
			updateWrapper.eq("id",agentDrawApply.getId());
			agentDrawApply.setStatus(4);

			int upCnt = baseMapper.update(agentDrawApply,updateWrapper);
			if(upCnt ==0){//更新失败
				return "该申请状态已经变更！";

			}
			//增加账户余额
			SysAgent agent = sysAgentService.getById(SessionUtil.getAgentId());
			sysAgentService.updateBal(agentDrawApply.getAgentId(),Constant.InOutEnum.IN.getCode().toString(),agentDrawApply.getAmount());
			//删除交易明细
			UpdateWrapper updateWrapper1 = new UpdateWrapper();
			updateWrapper1.eq("tran_type",Constant.AgentAccountTranTypeEnum.DRAW.getCode());
			updateWrapper1.eq("agent_id",agentDrawApply.getAgentId());
			updateWrapper1.eq("tran_id",agentDrawApply.getId());
			AgentAccountDetail detail = new AgentAccountDetail();
			detail.setStatus(Constant.DeleteStateEnum.DELETE.getCode().toString());
			agentAccountDetailService.update(detail,updateWrapper1);


		}else{

			return "该申请状态已经变更！";
		}
		return "success";

	}

}

